This article mainly introduces node. use q. js implements api promise. promise is a standard that describes the returned results of asynchronous calls, including correct returned results and error handling, need a friend can refer to what is promise and
This article describes how to use Promise in Node. js to encapsulate asynchronous functions and use unified chained APIs to get rid of the nightmare of multiple callbacks. it is a very practical little skill, I hope my friends will like writing Node. during the js process, c
functions are called, in addition, a promise can even accept a new callback after the implementation is resolved. These callbacks are done in a normal way, allowing us to use callbacks to implement a simple form of caching:vartaskspromise;functiongettasks () {taskpromise= Taskpromise | |Gettasksfromtheserver (); returnTaskpromise;} In this case, the Gettasks () function can be called any number of times, and it always returns the
Use q. js in node. js to implement api promise
Here we will look at how to use q. js to implement node api promise.
I. Everything starts with install
The Code is as follows:
Npm insta
-- Address: http://blog.chrisyip.im/nodejs-101-package-promise-and-async
First, let's review the Sagase project structure:
lib/ cli.js sagase.jsGruntfile.jspackage.json
I spoke about it in the previous article.package.json, This articlelib/sagase.js.
Because the code is relatively long, we will talk about it separately in one section. For the complete code, open GitHub.
'use strict';
Notifies the compiler to enter strict mode. The main function
try...catch when the status is reject in the promise returned by sleep.So what the hell is going on here? Let's see a picture.This code is just like the code, only after the async function is called and output a "main program is not called", the result is as followsWe find that the output is printed first, which seems to be different from our code, what's going on here.In general async/await is the promise
Copyright NOTICE: This work is licensed under the Creative Commons Attribution-NonCommercial use-Share 4.0 International license agreement in the same way. Reprint please indicate source Http://blog.csdn.net/azureterniteDirectory (?) [+]The concept of promise
The Promise object is used for asynchronous (asynchronous) computations: A Promise object repres
above, promise itself is an event publish/subscribe model.So the three functions above then are equivalent to the event binding function. (is the observer)When the previous task is deferred.resolve (data), corresponding to this task will trigger the Onfulfilled method.When the previous task is deferred.reject (err), the corresponding task will trigger the Onrejected method.Any one task, the onfulfilled or Onrejected method can only be triggered one,
Tags: values from error message factory and nested read JSON messageRecently, while learning about node. js, I've heard that the callback hell results have been around for a week. So take the time to learn a little bit about promise. Although there are async/await, CO, generators and other options, but because I base poor, and time problems so decided to use good
{ //Ball on the left if(MarginLeftdistance) {MarginLeft++ }Else{ //the ball is on the rightmarginleft-- } //adjust the position of the ballBall.style.marginLeft=MarginLeft animate (ball, distance, CD)}, -) } //Control AnimationsAnimate (Ball1, -,function() {animate (Ball2, $, function() {animate (Ball3, Max, function() {animate (Ball2, Max, function() {animate (Ball
Jintiansheng: To learn a new thing, it is necessary to hold a hug mentality, if cling to their previous concept system, there will be a difficult feeling.. NET programmer first with node. JS is the most need to adapt to asynchronous development, all asynchronous, the general logic of the traversal of the list is asynchronous, how to ensure that the list traversal execution is complete?
Here we will look at how to use q. js to implement node api promise.1. Everything starts with installNpm install q2. Standard node style api promise method1. Use Q. nfcallCompared with Q. fcall, Q. nfcall is the node's Q. fcal
Page Pages.foreach (function (HTML) {//parsing HTML varcourses=filterchapters (HTML) Coursesdata.push (Courses)})//Traverse Coursesdata.sort (function (A, b) {returna.numberThe results of the operation are as follows:650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/8F/50/wKiom1jahlPyvW7uAABnQMXU45E114.jpg "title=" 36020170315204858576.jpg "alt=" Wkiom1jahlpyvw7uaabnqmxu45e114.jpg "/>This article is from the "It Rookie" blog, make sure to keep this source http://mazongfei.blog.51cto.c
Implement the promises library of nodejs Based on promise. js, promise. jsnodejs
Today, I downloaded promise. js from the GIT source code library and found that the source code is written based on Web Front-end JavaScript and cannot be directly used for nodejs. Fortunately,
The most recent way to implement mock virtual data through node is to use the FS module of node to read and write to our local files to achieve a mock function:
We can develop a JS file that specializes in encapsulating a simple file-reading function.
The JS code is as follows:
Let FS = require (' FS '); Let DEALFN =
PromiseThe motives for studying Promise are broadly as follows:
Unfamiliar with its API and curiosity about the implementation mechanism;
Many libraries (such as fetch) are based on the Promise package, so to understand the preconditions of these libraries first familiar with Promise;
To understand other more advanced asynchronous operations to b
It's enough to talk about js promise. It's enough to talk about jspromise.
I. background
We all know that nodejs is very fast. Why is it so fast? The reason is that node uses asynchronous callback to process the events that need to be waited, so that the Code will continue to be executed without waiting somewhere. But there is also a bad thing. When we have a lot
The previous period of work, because the project to the front-end implementation of storage, so the use of Websql, and Websql API involves a lot of asynchronous problems, if the way to take the callback function, the code is not elegant, and is not conducive to understanding, so they found the promise, After the use of some of their own understanding and experience, to share with you in this article.What is promis
second parameter of then, used to specify the callback of the reject, the usage is this:GetNumber (). Then (function (data) { Console.log (' resolved '); Console.log (data);}). catch (function (reason) { console.log (' rejected '); Console.log (reason);});The effect is the same as the second parameter written in then. However, it has another function: When executing the resolve callback (that is, the first argument in the above then), if the exception is thrown (the code is wrong),
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.